package com.gudaichaoren.spider.algorithm;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gudaichaoren.spider.constant.Constants;
import com.gudaichaoren.spider.entity.LotteriesShuangseCalculate;
import com.gudaichaoren.spider.model.request.QueryWinningRequest;
import com.gudaichaoren.spider.model.response.LotteriesShuangseCalculateRes;
import com.gudaichaoren.spider.service.LotteriesShuangseAnalysisService;
import com.gudaichaoren.spider.service.LotteriesShuangseCalculateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class LotteriesShuangseAlgorithm {

    @Autowired
    private LotteriesShuangseAnalysisService lotteriesShuangseAnalysisService;

    @Autowired
    private LotteriesShuangseCalculateService lotteriesShuangseCalculateService;

    public void Calculate() {
        //查询出当前遗漏的分析数据
        lotteriesShuangseAnalysisService.Calculate(Constants.LotteriesShuangseAnalysisTypeEnum.当前遗漏);
    }

    /**
     * 查询出中奖的号码:{"period":"2020100","redBall":"01,07,15,16,20,23","blueBall":"07"}
     * 中奖情况
     * 一等奖：6+1
     * 二等奖：6
     * 三等奖：5+1
     * 四等奖：5+0，4+1
     * 五等奖：4+0,3+1
     * 六等奖：2+1,1+1,0+1
     * @param queryWinningRequest
     */
    public ArrayList<LotteriesShuangseCalculateRes> queryWinning(QueryWinningRequest queryWinningRequest) {
        ArrayList<LotteriesShuangseCalculateRes> lotteriesShuangseCalculateList = new ArrayList<>();
        //查询出当前的期号计算球
        List<LotteriesShuangseCalculate> calculateList = lotteriesShuangseCalculateService.list(new QueryWrapper<LotteriesShuangseCalculate>().eq("period", queryWinningRequest.getPeriod()));
        for (LotteriesShuangseCalculate lotteriesShuangseCalculate : calculateList) {

            String[] redBallsResult = queryWinningRequest.getRedBall().split(",");

            String[] redBallsCalculate = {lotteriesShuangseCalculate.getRedBallNumber01(), lotteriesShuangseCalculate.getRedBallNumber02(), lotteriesShuangseCalculate.getRedBallNumber03(), lotteriesShuangseCalculate.getRedBallNumber04(), lotteriesShuangseCalculate.getRedBallNumber05(), lotteriesShuangseCalculate.getRedBallNumber06()};


            //一等奖：6+1
            if ((StrUtil.equals(lotteriesShuangseCalculate.getBlueBallNumber01(),queryWinningRequest.getBlueBall()))&&findSame(redBallsResult,redBallsCalculate)==6) {
                LotteriesShuangseCalculateRes lotteriesShuangseCalculateRes = new LotteriesShuangseCalculateRes();
                BeanUtil.copyProperties(lotteriesShuangseCalculate,lotteriesShuangseCalculateRes,true);
                lotteriesShuangseCalculateRes.setAwardsDesc(Constants.LotteriesShuangseAwards.FIRST_PRIZE);
                lotteriesShuangseCalculateList.add(lotteriesShuangseCalculateRes);
                continue;
            }
            //二等奖：6
            if (findSame(redBallsResult,redBallsCalculate)==6) {
                LotteriesShuangseCalculateRes lotteriesShuangseCalculateRes = new LotteriesShuangseCalculateRes();
                BeanUtil.copyProperties(lotteriesShuangseCalculate,lotteriesShuangseCalculateRes,true);
                lotteriesShuangseCalculateRes.setAwardsDesc(Constants.LotteriesShuangseAwards.SECOND_PRIZE);
                lotteriesShuangseCalculateList.add(lotteriesShuangseCalculateRes);
                continue;
            }
            //三等奖：5+1
            if ((StrUtil.equals(lotteriesShuangseCalculate.getBlueBallNumber01(),queryWinningRequest.getBlueBall()))&&findSame(redBallsResult,redBallsCalculate)==5) {
                LotteriesShuangseCalculateRes lotteriesShuangseCalculateRes = new LotteriesShuangseCalculateRes();
                BeanUtil.copyProperties(lotteriesShuangseCalculate,lotteriesShuangseCalculateRes,true);
                lotteriesShuangseCalculateRes.setAwardsDesc(Constants.LotteriesShuangseAwards.THIRD_PRIZE);
                lotteriesShuangseCalculateList.add(lotteriesShuangseCalculateRes);
                continue;
            }
            //四等奖：5+0，4+1
            if (((StrUtil.equals(lotteriesShuangseCalculate.getBlueBallNumber01(),queryWinningRequest.getBlueBall()))&&findSame(redBallsResult,redBallsCalculate)==4)||(findSame(redBallsResult,redBallsCalculate)==5)) {
                LotteriesShuangseCalculateRes lotteriesShuangseCalculateRes = new LotteriesShuangseCalculateRes();
                BeanUtil.copyProperties(lotteriesShuangseCalculate,lotteriesShuangseCalculateRes,true);
                lotteriesShuangseCalculateRes.setAwardsDesc(Constants.LotteriesShuangseAwards.FOURTH_PRIZE);
                lotteriesShuangseCalculateList.add(lotteriesShuangseCalculateRes);
                continue;
            }
            //五等奖：4+0,3+1
            if (((StrUtil.equals(lotteriesShuangseCalculate.getBlueBallNumber01(),queryWinningRequest.getBlueBall()))&&findSame(redBallsResult,redBallsCalculate)==3)||(findSame(redBallsResult,redBallsCalculate)==4)) {
                LotteriesShuangseCalculateRes lotteriesShuangseCalculateRes = new LotteriesShuangseCalculateRes();
                BeanUtil.copyProperties(lotteriesShuangseCalculate,lotteriesShuangseCalculateRes,true);
                lotteriesShuangseCalculateRes.setAwardsDesc(Constants.LotteriesShuangseAwards.FIFTH_PRIZE);
                lotteriesShuangseCalculateList.add(lotteriesShuangseCalculateRes);
                continue;
            }
            //六等奖：2+1,1+1,0+1
            if (((StrUtil.equals(lotteriesShuangseCalculate.getBlueBallNumber01(),queryWinningRequest.getBlueBall()))&&findSame(redBallsResult,redBallsCalculate)==2)||((StrUtil.equals(lotteriesShuangseCalculate.getBlueBallNumber01(),queryWinningRequest.getBlueBall()))&&findSame(redBallsResult,redBallsCalculate)==1)||(StrUtil.equals(lotteriesShuangseCalculate.getBlueBallNumber01(),queryWinningRequest.getBlueBall()))) {
                LotteriesShuangseCalculateRes lotteriesShuangseCalculateRes = new LotteriesShuangseCalculateRes();
                BeanUtil.copyProperties(lotteriesShuangseCalculate,lotteriesShuangseCalculateRes,true);
                lotteriesShuangseCalculateRes.setAwardsDesc(Constants.LotteriesShuangseAwards.SIXTH_PRIZE);
                lotteriesShuangseCalculateList.add(lotteriesShuangseCalculateRes);
                continue;
            }
        }
        return lotteriesShuangseCalculateList;
    }

    private int findSame(String[] arr1, String[] arr2) {
        int count = 0;
        if (arr1 == null || arr2 == null || arr1.length == 0 || arr2.length == 0) {
            return count;
        }
        for(int i=0;i<arr1.length;i++) {
            for(int j=0;j<arr2.length;j++)
                if(StrUtil.equals(arr1[i],arr2[j])) {
                    count++;
                    break;
                }
        }
        return count;
    }
}
